package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ChangeOrderPeopleEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.ui.activity.self.order.ChangeOrderPeopleActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangeOrderPeopleModel {
    private ChangeOrderPeopleActivity activity;
    private int size = 20;
    private int pageNum = 1;
    private int upPage = 1;
    public int total;

    public ChangeOrderPeopleModel(ChangeOrderPeopleActivity activity) {
        this.activity = activity;
    }

    //获取订单列表
    public void getOrderList(boolean loadMore) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        upPage = loadMore ? ++upPage : 1;
        pageNum = upPage;
        APIManager.getInstance().getOrderAPI().getOrderList(pageNum, size).enqueue(new Callback<BaseEntity<ChangeOrderPeopleEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ChangeOrderPeopleEntity>> call, Response<BaseEntity<ChangeOrderPeopleEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    total = response.body().getData().getTotalRow();
                    activity.showOrderList(response.body().getData().getList(), loadMore);
                }else {
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ChangeOrderPeopleEntity>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    //获取价格单位
    public void getPeopleModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().selectUnit().enqueue(new Callback<BaseEntity<List<UnitEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<UnitEntity>>> call, Response<BaseEntity<List<UnitEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.showPeople(response.body().getData());
                }else {
                    MToast.showToast(activity,response.body().getMsg() );
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<UnitEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
